using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace PipeCSharp
{
    public partial class MainForm : Form
    {
        private readonly List<string> _activePipeName = new List<string>();

        public MainForm()
        {
            InitializeComponent();
        }

        private void SearchButton_Click(object sender, EventArgs e)
        {
            _activePipeName.Clear();
            PipeListView.Items.Clear();
            Process[] processes = Process.GetProcessesByName("Keyboard");

            foreach (Process process in processes)
            {
                int result = NamedPipeClient.IsServerAvailable(string.Format("\\\\.\\pipe\\DInput-{0}", process.Id));
                _activePipeName.Add(string.Format("\\\\.\\pipe\\DInput-{0}", process.Id));
            }

            foreach (string pipeName in _activePipeName)
            {
                PipeListView.Items.Add(string.Format("{0} is active.\r\n", pipeName));
            }
        }

        private void TestButton_Click(object sender, EventArgs e)
        {
            NamedPipeClient namedPipeClient = new NamedPipeClient(_activePipeName[0]);
            namedPipeClient.Connect();
            KeyInfo keyInfo = new KeyInfo();
            keyInfo.dwOfs = DinputKeys.DIK_1;
            keyInfo.dwData = 0x80;
            for (int i = 0; i < 100; i++)
            {
                namedPipeClient.Write(keyInfo);
                Thread.Sleep(200);
            }
            namedPipeClient.Disconnect();
        }

        private void StartButton_Click(object sender, EventArgs e)
        {

        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            
        }
    }
}